<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>loosemock_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_loosemock_test.js.html">loosemock_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.LooseMockTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.LooseMockTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.testing.LooseMock&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.mockmatchers&#39;);
<a name="line22"></a>
<a name="line23"></a>// The object that we will be mocking
<a name="line24"></a>var RealObject = function() {
<a name="line25"></a>};
<a name="line26"></a>
<a name="line27"></a>RealObject.prototype.a = function() {
<a name="line28"></a>  fail(&#39;real object should never be called&#39;);
<a name="line29"></a>};
<a name="line30"></a>
<a name="line31"></a>RealObject.prototype.b = function() {
<a name="line32"></a>  fail(&#39;real object should never be called&#39;);
<a name="line33"></a>};
<a name="line34"></a>
<a name="line35"></a>var mock;
<a name="line36"></a>
<a name="line37"></a>var stubs;
<a name="line38"></a>
<a name="line39"></a>function setUp() {
<a name="line40"></a>  var obj = new RealObject();
<a name="line41"></a>  mock = new goog.testing.LooseMock(obj);
<a name="line42"></a>  stubs = new goog.testing.PropertyReplacer();
<a name="line43"></a>}
<a name="line44"></a>
<a name="line45"></a>function tearDown() {
<a name="line46"></a>  stubs.reset();
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>/*
<a name="line50"></a> * Calling this method evades the logTestFailure in
<a name="line51"></a> * goog.testing.Mock.prototype.$recordAndThrow, so it doesn&#39;t look like the
<a name="line52"></a> * test failed.
<a name="line53"></a> */
<a name="line54"></a>function silenceFailureLogging() {
<a name="line55"></a>  if (goog.global[&#39;G_testRunner&#39;]) {
<a name="line56"></a>    stubs.set(goog.global[&#39;G_testRunner&#39;],
<a name="line57"></a>        &#39;logTestFailure&#39;, goog.nullFunction);
<a name="line58"></a>  }
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function unsilenceFailureLogging() {
<a name="line62"></a>  stubs.reset();
<a name="line63"></a>}
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * Version of assertThrows that doesn&#39;t log the exception generated by the
<a name="line68"></a> * mocks under test.
<a name="line69"></a> * TODO: would be nice to check that a particular substring is in the thrown
<a name="line70"></a> * message so we know it&#39;s not something dumb like a syntax error
<a name="line71"></a> */
<a name="line72"></a>function assertThrowsQuiet(var_args) {
<a name="line73"></a>  silenceFailureLogging();
<a name="line74"></a>  assertThrows.apply(null, arguments);
<a name="line75"></a>  unsilenceFailureLogging();
<a name="line76"></a>}
<a name="line77"></a>
<a name="line78"></a>// Most of the basic functionality is tested in strictmock_test; these tests
<a name="line79"></a>// cover the cases where loose mocks are different from strict mocks
<a name="line80"></a>function testSimpleExpectations() {
<a name="line81"></a>  mock.a(5);
<a name="line82"></a>  mock.b();
<a name="line83"></a>  mock.$replay();
<a name="line84"></a>  mock.a(5);
<a name="line85"></a>  mock.b();
<a name="line86"></a>  mock.$verify();
<a name="line87"></a>
<a name="line88"></a>  mock.$reset();
<a name="line89"></a>
<a name="line90"></a>  mock.a();
<a name="line91"></a>  mock.b();
<a name="line92"></a>  mock.$replay();
<a name="line93"></a>  mock.b();
<a name="line94"></a>  mock.a();
<a name="line95"></a>  mock.$verify();
<a name="line96"></a>
<a name="line97"></a>  mock.$reset();
<a name="line98"></a>
<a name="line99"></a>  mock.a(5).$times(2);
<a name="line100"></a>  mock.a(5);
<a name="line101"></a>  mock.a(2);
<a name="line102"></a>  mock.$replay();
<a name="line103"></a>  mock.a(5);
<a name="line104"></a>  mock.a(5);
<a name="line105"></a>  mock.a(5);
<a name="line106"></a>  mock.a(2);
<a name="line107"></a>  mock.$verify();
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>function testMultipleExpectations() {
<a name="line112"></a>  mock.a().$returns(1);
<a name="line113"></a>  mock.a().$returns(2);
<a name="line114"></a>  mock.$replay();
<a name="line115"></a>  assertEquals(1, mock.a());
<a name="line116"></a>  assertEquals(2, mock.a());
<a name="line117"></a>  mock.$verify();
<a name="line118"></a>}
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>function testMultipleExpectationArgs() {
<a name="line122"></a>  mock.a(&#39;asdf&#39;).$anyTimes();
<a name="line123"></a>  mock.a(&#39;qwer&#39;).$anyTimes();
<a name="line124"></a>  mock.b().$times(3);
<a name="line125"></a>  mock.$replay();
<a name="line126"></a>  mock.a(&#39;asdf&#39;);
<a name="line127"></a>  mock.b();
<a name="line128"></a>  mock.a(&#39;asdf&#39;);
<a name="line129"></a>  mock.a(&#39;qwer&#39;);
<a name="line130"></a>  mock.b();
<a name="line131"></a>  mock.a(&#39;qwer&#39;);
<a name="line132"></a>  mock.b();
<a name="line133"></a>  mock.$verify();
<a name="line134"></a>
<a name="line135"></a>  mock.$reset();
<a name="line136"></a>
<a name="line137"></a>  mock.a(&#39;asdf&#39;).$anyTimes();
<a name="line138"></a>  mock.a(&#39;qwer&#39;).$anyTimes();
<a name="line139"></a>  mock.$replay();
<a name="line140"></a>  mock.a(&#39;asdf&#39;);
<a name="line141"></a>  mock.a(&#39;qwer&#39;);
<a name="line142"></a>  goog.bind(mock.a, mock, &#39;asdf&#39;);
<a name="line143"></a>  goog.bind(mock.$verify, mock);
<a name="line144"></a>}
<a name="line145"></a>
<a name="line146"></a>function testSameMethodOutOfOrder() {
<a name="line147"></a>  mock.a(&#39;foo&#39;).$returns(1);
<a name="line148"></a>  mock.a(&#39;bar&#39;).$returns(2);
<a name="line149"></a>  mock.$replay();
<a name="line150"></a>  assertEquals(2, mock.a(&#39;bar&#39;));
<a name="line151"></a>  assertEquals(1, mock.a(&#39;foo&#39;));
<a name="line152"></a>}
<a name="line153"></a>
<a name="line154"></a>function testSameMethodDifferentReturnValues() {
<a name="line155"></a>  mock.a(&#39;foo&#39;).$returns(1).$times(2);
<a name="line156"></a>  mock.a(&#39;foo&#39;).$returns(3);
<a name="line157"></a>  mock.a(&#39;bar&#39;).$returns(2);
<a name="line158"></a>  mock.$replay();
<a name="line159"></a>  assertEquals(1, mock.a(&#39;foo&#39;));
<a name="line160"></a>  assertEquals(2, mock.a(&#39;bar&#39;));
<a name="line161"></a>  assertEquals(1, mock.a(&#39;foo&#39;));
<a name="line162"></a>  assertEquals(3, mock.a(&#39;foo&#39;));
<a name="line163"></a>  assertThrowsQuiet(function() {
<a name="line164"></a>    mock.a(&#39;foo&#39;);
<a name="line165"></a>    mock.$verify();
<a name="line166"></a>  });
<a name="line167"></a>}
<a name="line168"></a>
<a name="line169"></a>function testSameMethodBrokenExpectations() {
<a name="line170"></a>  // This is a weird corner case.
<a name="line171"></a>  // No way to ever make this verify no matter what you call after replaying,
<a name="line172"></a>  // because the second expectation of mock.a(&#39;foo&#39;) will be masked by
<a name="line173"></a>  // the first expectation that can be called any number of times, and so we
<a name="line174"></a>  // can never satisfy that second expectation.
<a name="line175"></a>  mock.a(&#39;foo&#39;).$returns(1).$anyTimes();
<a name="line176"></a>  mock.a(&#39;bar&#39;).$returns(2);
<a name="line177"></a>  mock.a(&#39;foo&#39;).$returns(3);
<a name="line178"></a>
<a name="line179"></a>  // LooseMock can detect this case and fail on $replay.
<a name="line180"></a>  assertThrowsQuiet(goog.bind(mock.$replay, mock));
<a name="line181"></a>  mock.$reset();
<a name="line182"></a>
<a name="line183"></a>  // This is a variant of the corner case above, but it&#39;s harder to determine
<a name="line184"></a>  // that the expectation to mock.a(&#39;bar&#39;) can never be satisfied. So we don&#39;t
<a name="line185"></a>  // fail on $replay, but we do fail on $verify.
<a name="line186"></a>  mock.a(goog.testing.mockmatchers.isString).$returns(1).$anyTimes();
<a name="line187"></a>  mock.a(&#39;bar&#39;).$returns(2);
<a name="line188"></a>  mock.$replay();
<a name="line189"></a>
<a name="line190"></a>  assertEquals(1, mock.a(&#39;foo&#39;));
<a name="line191"></a>  assertEquals(1, mock.a(&#39;bar&#39;));
<a name="line192"></a>  assertThrowsQuiet(goog.bind(mock.$verify, mock));
<a name="line193"></a>}
<a name="line194"></a>
<a name="line195"></a>function testSameMethodMultipleAnyTimes() {
<a name="line196"></a>  mock.a(&#39;foo&#39;).$returns(1).$anyTimes();
<a name="line197"></a>  mock.a(&#39;foo&#39;).$returns(2).$anyTimes();
<a name="line198"></a>  mock.$replay();
<a name="line199"></a>  assertEquals(1, mock.a(&#39;foo&#39;));
<a name="line200"></a>  assertEquals(1, mock.a(&#39;foo&#39;));
<a name="line201"></a>  assertEquals(1, mock.a(&#39;foo&#39;));
<a name="line202"></a>  // Note we&#39;ll never return 2 but that&#39;s ok.
<a name="line203"></a>  mock.$verify();
<a name="line204"></a>}
<a name="line205"></a>
<a name="line206"></a>function testFailingFast() {
<a name="line207"></a>  mock.a().$anyTimes();
<a name="line208"></a>  mock.$replay();
<a name="line209"></a>  mock.a();
<a name="line210"></a>  mock.a();
<a name="line211"></a>  assertThrowsQuiet(goog.bind(mock.b, mock));
<a name="line212"></a>  mock.$reset();
<a name="line213"></a>
<a name="line214"></a>  // too many
<a name="line215"></a>  mock.a();
<a name="line216"></a>  mock.b();
<a name="line217"></a>  mock.$replay();
<a name="line218"></a>  mock.a();
<a name="line219"></a>  mock.b();
<a name="line220"></a>
<a name="line221"></a>  var message;
<a name="line222"></a>  silenceFailureLogging();
<a name="line223"></a>  try {
<a name="line224"></a>    mock.a();
<a name="line225"></a>  } catch (e) {
<a name="line226"></a>    message = e.message;
<a name="line227"></a>  }
<a name="line228"></a>  unsilenceFailureLogging();
<a name="line229"></a>
<a name="line230"></a>  assertTrue(&#39;No exception thrown on unexpected call&#39;, goog.isDef(message));
<a name="line231"></a>  assertContains(&#39;Too many calls to a&#39;, message);
<a name="line232"></a>}
<a name="line233"></a>
<a name="line234"></a>function testTimes() {
<a name="line235"></a>  mock.a().$times(3);
<a name="line236"></a>  mock.b().$times(2);
<a name="line237"></a>  mock.$replay();
<a name="line238"></a>  mock.a();
<a name="line239"></a>  mock.b();
<a name="line240"></a>  mock.b();
<a name="line241"></a>  mock.a();
<a name="line242"></a>  mock.a();
<a name="line243"></a>  mock.$verify();
<a name="line244"></a>}
<a name="line245"></a>
<a name="line246"></a>
<a name="line247"></a>function testFailingSlow() {
<a name="line248"></a>  // not enough
<a name="line249"></a>  mock.a().$times(3);
<a name="line250"></a>  mock.$replay();
<a name="line251"></a>  mock.a();
<a name="line252"></a>  mock.a();
<a name="line253"></a>  assertThrowsQuiet(goog.bind(mock.$verify, mock));
<a name="line254"></a>
<a name="line255"></a>  mock.$reset();
<a name="line256"></a>
<a name="line257"></a>  // not enough, interleaved order
<a name="line258"></a>  mock.a().$times(3);
<a name="line259"></a>  mock.b().$times(3);
<a name="line260"></a>  mock.$replay();
<a name="line261"></a>  mock.a();
<a name="line262"></a>  mock.b();
<a name="line263"></a>  mock.a();
<a name="line264"></a>  mock.b();
<a name="line265"></a>  assertThrowsQuiet(goog.bind(mock.$verify, mock));
<a name="line266"></a>
<a name="line267"></a>  mock.$reset();
<a name="line268"></a>  // bad args
<a name="line269"></a>  mock.a(&#39;asdf&#39;).$anyTimes();
<a name="line270"></a>  mock.$replay();
<a name="line271"></a>  mock.a(&#39;asdf&#39;);
<a name="line272"></a>  assertThrowsQuiet(goog.bind(mock.a, mock, &#39;qwert&#39;));
<a name="line273"></a>  assertThrowsQuiet(goog.bind(mock.$verify, mock));
<a name="line274"></a>}
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>function testArgsAndReturns() {
<a name="line278"></a>  mock.a(&#39;asdf&#39;).$atLeastOnce().$returns(5);
<a name="line279"></a>  mock.b(&#39;qwer&#39;).$times(2).$returns(3);
<a name="line280"></a>  mock.$replay();
<a name="line281"></a>  assertEquals(5, mock.a(&#39;asdf&#39;));
<a name="line282"></a>  assertEquals(3, mock.b(&#39;qwer&#39;));
<a name="line283"></a>  assertEquals(5, mock.a(&#39;asdf&#39;));
<a name="line284"></a>  assertEquals(5, mock.a(&#39;asdf&#39;));
<a name="line285"></a>  assertEquals(3, mock.b(&#39;qwer&#39;));
<a name="line286"></a>  mock.$verify();
<a name="line287"></a>}
<a name="line288"></a>
<a name="line289"></a>
<a name="line290"></a>function testThrows() {
<a name="line291"></a>  mock.a().$throws(&#39;exception!&#39;);
<a name="line292"></a>  mock.$replay();
<a name="line293"></a>  assertThrowsQuiet(goog.bind(mock.a, mock));
<a name="line294"></a>  mock.$verify();
<a name="line295"></a>}
<a name="line296"></a>
<a name="line297"></a>
<a name="line298"></a>function testDoes() {
<a name="line299"></a>  mock.a(1, 2).$does(function(a, b) {return a + b;});
<a name="line300"></a>  mock.$replay();
<a name="line301"></a>  assertEquals(&#39;Mock should call the function&#39;, 3, mock.a(1, 2));
<a name="line302"></a>  mock.$verify();
<a name="line303"></a>}
<a name="line304"></a>
<a name="line305"></a>function testIgnoresExtraCalls() {
<a name="line306"></a>  mock = new goog.testing.LooseMock(RealObject, true);
<a name="line307"></a>  mock.a();
<a name="line308"></a>  mock.$replay();
<a name="line309"></a>  mock.a();
<a name="line310"></a>  mock.b(); // doesn&#39;t throw
<a name="line311"></a>  mock.$verify();
<a name="line312"></a>}
<a name="line313"></a>
<a name="line314"></a>function testSkipAnyTimes() {
<a name="line315"></a>  mock = new goog.testing.LooseMock(RealObject);
<a name="line316"></a>  mock.a(1).$anyTimes();
<a name="line317"></a>  mock.a(2).$anyTimes();
<a name="line318"></a>  mock.a(3).$anyTimes();
<a name="line319"></a>  mock.$replay();
<a name="line320"></a>  mock.a(1);
<a name="line321"></a>  mock.a(3);
<a name="line322"></a>  mock.$verify();
<a name="line323"></a>}
<a name="line324"></a>
<a name="line325"></a>function testErrorMessageForBadArgs() {
<a name="line326"></a>  mock.a();
<a name="line327"></a>  mock.$anyTimes();
<a name="line328"></a>
<a name="line329"></a>  mock.$replay();
<a name="line330"></a>
<a name="line331"></a>  var message;
<a name="line332"></a>  silenceFailureLogging();
<a name="line333"></a>  try {
<a name="line334"></a>    mock.a(&#39;a&#39;);
<a name="line335"></a>  } catch (e) {
<a name="line336"></a>    message = e.message;
<a name="line337"></a>  }
<a name="line338"></a>  unsilenceFailureLogging();
<a name="line339"></a>
<a name="line340"></a>  assertTrue(&#39;No exception thrown on verify&#39;, goog.isDef(message));
<a name="line341"></a>  assertContains(&#39;Bad arguments to a()&#39;, message);
<a name="line342"></a>}
</pre>


</body>
</html>
